package com.dominator.utils.dateutil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 获取两个月之间所有月份的集合
     * @param minDate
     * @param maxDate
     * @return
     */
    public  static List<String> getMonthBetween(String minDate, String maxDate)  {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        try {
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("DateUtil  have some error" + e);
        }

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 日期类型转换为string类型
     * @return
     */
    public static String  DatetoString(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
        String str=sdf.format(date);
        return str;
    }

    /**
     * 根据给定的日期格式转换日期
     * @param date
     * @param format
     * @return
     */
    public static String DatetoStringFormat(Date date ,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        String str=sdf.format(date);
        return str;
    }

    /**
     * 根据给定的格式字符串转换成date
     * @param str
     * @return
     */
    public static Date StringToDate(String str){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date=sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static void main(String[] args) {
        Date date1 = new Date();
        String s =  DatetoStringFormat(date1,"yyyy年MM月hh日 HH:mm");
        System.out.printf(s);
    }

}
